package com.slt.concurrency.mashibing2019.juc.c_007_LockAndTools.exercise;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLock_reentrant {
    Lock reentrantLock = new ReentrantLock();

    private  void m1() {
        try {
            reentrantLock.lock();
            for (int i = 0; i < 5; i++) {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(i);
                if(i == 2){
                    m2();
                }
            }
        }catch (InterruptedException exception){
            exception.printStackTrace();
        }finally {
            System.out.println("unlock");
            reentrantLock.unlock();
        }
    }

    private void m2(){
        try {
            reentrantLock.lock();
            System.out.println("m2 ...");
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        ReentrantLock_reentrant reentrant = new ReentrantLock_reentrant();
        new Thread(reentrant::m1).start();
    }
}
